package com.tcts;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.sound.midi.SysexMessage;
import javax.swing.text.html.HTML;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.HttpWebConnection;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class DealService {

	private static final String LOCAL_HOST_ONE = "tctsthrough.com";//"tctsthrough.wicp.net";//.com";
	private static final String LOCAL_HOST_TWO = "tctsthrough.cn";
	private static final String TARGET_HOST_ONE = "mogujie.com";
	private static final String TARGET_HOST_TWO = "mogujie.cn";
	//private static final String TARGET_HOST = "http://shop.mogujie.com/";
	private static String strIp = ""; 
	
	public DealService(){ 
	} 

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String arch = System.getProperty("sun.arch.data.model");
		System.out.println(arch);
		String string = "/search/";
		string = string.replace("/search/", "http://www.tctsthrough.com/search/");
		System.out.println(string);
	}
 
	public void mgetPage(String url, PrintWriter out)
			throws java.io.IOException, ServletException {
		try {   
			//if(url.contains(LOCAL_HOST_TWO))
			//	url=url.replace(LOCAL_HOST_TWO, TARGET_HOST_TWO); 　 
			InputStream inputStream = getInputStream(url); 
			if (inputStream == null)
				out.println("服务器请求异常");
			// Convert Stream to String 
			String responseStr = toStringAndProcessBeforeReturn(inputStream);
			out.println(responseStr);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("请求异常");
		}
		System.out.println("请求成功");
	}
 
	public InputStream getInputStream(String orginal_url) {
		try { 
			URL url = new URL(orginal_url);
			HttpURLConnection urlConnection = (HttpURLConnection) url
					.openConnection();
			// GET Request Define:
			urlConnection.setRequestMethod("GET");
			urlConnection.setRequestProperty("contentType", "UTF-8");
			urlConnection.connect(); 
			InputStream inputStream = urlConnection.getInputStream();
			return inputStream;
		} catch (Exception e) {
			System.out.print("请求异常");
			e.printStackTrace();
		}
		return null;
	}

	public String toStringAndProcessBeforeReturn(InputStream inputStream) {
		int count =0;
		Scanner sc = new Scanner(inputStream, "UTF-8");
		StringBuilder result = new StringBuilder();
		while (sc.hasNext()) { 
			String replace2local_str = sc.nextLine();//.replace(TARGET_HOST,LOCAL_HOST);
			if(replace2local_str.contains(TARGET_HOST_ONE)&&!replace2local_str.contains("<link"))
				replace2local_str=replace2local_str.replace(TARGET_HOST_ONE,LOCAL_HOST_ONE); 
			//if(replace2local_str.contains(TARGET_HOST_TWO)&&!replace2local_str.contains("<link"))
				//replace2local_str=replace2local_str.replace(TARGET_HOST_TWO,LOCAL_HOST_TWO); 
			if(replace2local_str.contains("uinfo_wrap clearfix fm960")){
				removeDiv(sc,"div");
				continue;
			}
			
			result.append(replace2local_str + "\n");
		}
		return result.toString();
	}
	
	private Scanner removeDiv(Scanner sc,String type){
		int i=1;
		while(sc.hasNext()&&i!=0){
			String s =sc.nextLine(); 
			//System.err.println(s);
			if(s.contains("<"+type+"")&&!s.contains("</"+type+">"))
				i++;
			else if(!s.contains("<"+type+"")&&s.contains("</"+type+">"))
				i--; 
		}
		return sc;
	}
	
	
}
